package
{
	import flash.display.DisplayObject;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	import mymvc.event.BulletLeaveBorderEvent;
	import mymvc.observer.ObserverData;
	import mymvc.observer.ObserverManager;
	import mymvc.ui.BaseBehaviour;
	import mymvc.ui.BulletBehaviour;
	import mymvc.ui.UIManager;
	import mymvc.util.Logger;
	
	public class Thunder extends Sprite
	{
		private var logger:Logger = new Logger(this);
		private var fighterClass:Class = null;
		private var bullets:Array  = null;
		private var bulletClass:Class = null;
		private var fighter:Sprite = null;
		
		public function Thunder()
		{
			ObserverManager.getInstance().addObserver(
				"Fighter",this,this.fighterInit);
			ObserverManager.getInstance().addObserver(
				"Bullet",this,this.bulletInit);
			init();
			initSatgeSence();

		}
		private function init():void{
			logger.debug(this.numChildren);
			var uiManager:UIManager = UIManager.getInstance(this);
			uiManager.load("mymvc/swf/fighter.swf","Fighter");
			uiManager.load("mymvc/swf/bullet.swf","Bullet");
			initEvent();
			bullets = new Array();
		}
		
		private function fighterInit(data:ObserverData):void{
			fighterClass = data.getData("Fighter") as Class;
			
			fighter = new fighterClass();
			addChild(fighter);
			fighter.width=50;
			fighter.height=50;
			BaseBehaviour.moveCenterPoint(fighter,stage.stageWidth / 2,stage.stageHeight/2);
			BaseBehaviour.moveByKey(fighter);
			//BaseBehaviour.moveByMouse(fighter);
			
		}
		
		private function bulletInit(data:ObserverData):void{
			bulletClass = data.getData("Bullet") as Class;
			
			for(var i:int=0;i<30;i++){
				var bullet:Sprite = new bulletClass();
				bullet.visible = false;
				bullets.push(bullet);
			}
		}
		
		public function initSatgeSence():void{
			this.graphics.beginFill(0x000000);
			this.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			this.graphics.endFill();
		}
		
		private function shoot():void{
			var bulletVX:int = 0;
			var bulletVY:int = 0;
			var bullet:Sprite = bullets.pop() as Sprite;
			trace(bullet);
			this.addChild(bullet);
			BaseBehaviour.moveCenterPoint(bullet,
				fighter.x + fighter.width/2,
				fighter.y + fighter.height/2);
			var bulletBehaviour:BulletBehaviour = new BulletBehaviour(bullet);
			bullet.visible = true;
			trace('bullet show');
			bulletBehaviour.vy = 8;
			bulletBehaviour.moving();
			
			bullet.addEventListener(BulletLeaveBorderEvent.BULLET_LEAVE_BORDER,onBulletLeave);
		}
		
		private function initEvent():void{
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
		}
		//监听空格键打出子弹
		private function onKeyDown(event:KeyboardEvent):void{
			if(event.keyCode == Keyboard.SPACE){
				trace('shoot');
				shoot();
			}
		}
		//对game的屏幕进行检测
		private function onBulletLeave(event:BulletLeaveBorderEvent):void{
			var bullet:Sprite = event.target as Sprite;
			bullet.y = fighter.y + fighter.height /2;
			bullet.visible = false;
			bullets.push(bullet);
		} 
	}
}